﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForumServer;
using ForumServer.Forum;
using log4net;
using ForumLibrary;
using ForumLibrary.Data;

namespace ForumGUI
{
    public partial class ThreadsFrame : Form
    {
        private ForumManager _manager;
        private String _forum;
        private ClientController _controller;
        public ThreadsFrame(ClientController controller, ref ForumManager manager, String forumName)
        {
            InitializeComponent();
            _controller = controller;
            _controller.pushForm(this);
            _manager = manager;
            _forum = forumName;
            Title.Text = "Forum " + forumName;
            refreshThreads();
        }


        public String ForumName { get { return _forum; } }

        public void refreshThreads()
        {
            ThreadList.Items.Clear();
            String toParse = "";
            try
            {
                toParse = (string)_manager.getThreads(_forum);
            }
            catch
            {
                _controller.Logger.Error("Could not get Thread Headers");
                MessageBox.Show("Server is down. Please Logout and try again in a few minutes");
            }
            String[] headers = toParse.Split('~');
            foreach (String s in headers)            
                if (!String.IsNullOrEmpty(s))
                    ThreadList.Items.Add(s);            
        }
        private void addThread_Click(object sender, EventArgs e)
        {  
            this.Visible = false;
            new AddEditThread(_controller, ref _manager, _forum, true).Visible = true;
        }
        private void RemoveThread_Click(object sender, EventArgs e)
        {
            if (ThreadList.SelectedIndex < 0 || ThreadList.SelectedIndex > ThreadList.Items.Count - 1)
            {
                MessageBox.Show("Please select thread!!!");
                return;
            }
            DialogResult res = MessageBox.Show("Are you sure you want to delete this thread?", "Delete thread", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (res == System.Windows.Forms.DialogResult.Yes)
            {
                String ans = "";
                try
                {
                    ans = _manager.deleteThread(_controller.Username, _forum, (String)ThreadList.SelectedItem);
                }
                catch
                {
                    _controller.Logger.Error(_controller.Username + " Could not Delete Thread");
                    MessageBox.Show("Server is down. Please Logout and try again in a few minutes");
                }
                refreshThreads();
                MessageBox.Show(ans);
            }
        }

        private void ReturnButton_Click(object sender, EventArgs e)
        {
            this.Dispose(false);
            _controller.popForm().Visible = true;   
        }

        private void ViewThread_Click(object sender, EventArgs e)
        {
            if (ThreadList.SelectedIndex < 0 || ThreadList.SelectedIndex > ThreadList.Items.Count - 1)
            {
                MessageBox.Show("Please select thread!!!");
                return;
            }
            this.Visible = false;
            new PostsFrame(_controller, ref _manager, _forum, ThreadList.Items[ThreadList.SelectedIndex].ToString()).Visible = true;
        }

        delegate void RefeshView();
        public void update(ForumLibrary.Message messageType, string message)
        {
            if (this.ThreadList.InvokeRequired)
            {
                RefeshView updateView = new RefeshView(refreshThreads);
                this.Invoke(updateView, new object[] { });
            }
            else
            {
                refreshThreads();
            }
        }

        private void EditButton_Click(object sender, EventArgs e)
        {
            if (ThreadList.SelectedIndex < 0 || ThreadList.SelectedIndex > ThreadList.Items.Count - 1)
            {
                MessageBox.Show("Please select thread!!!");
                return;
            }
            this.Visible = false;
            new AddEditThread(_controller, ref _manager, _forum,false
                , (String)ThreadList.Items[ThreadList.SelectedIndex]).Visible = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            new ProfileFrame(ref _manager, ref _controller, _controller.Username).Visible = true;
        }

        private void ThreadList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.ViewThread_Click(null, null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.refreshThreads();
        }

    }
}
